package com.demo.lock.jdk;

public class DeadLock {

    static class Thread1 extends Thread {
        private Object res01;
        private Object res02;

        public Thread1(Object res01, Object res02) {
            this.res01 = res01;
            this.res02 = res02;
        }

        @Override
        public void run() {
            synchronized (res01) {
                System.out.println("thread1 locked res01");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (res02) {
                    System.out.println("Thread1 locked res02");
                }
            }


        }
    }

    static class Thread2 extends Thread {
        private Object res01;
        private Object res02;

        public Thread2(Object res01, Object res02) {
            this.res01 = res01;
            this.res02 = res02;
        }

        @Override
        public void run() {
            synchronized (res02) {
                System.out.println("Thread2 lcoked res02");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (res01) {
                    System.out.println("Thread2 locked res01");
                }
            }

        }
    }


}

class DeadLockTest {
    public static void main(String[] args) throws Exception {
        final Object res01 = "res01";
        final Object res02 = "res02";
        DeadLock.Thread1 t1 = new DeadLock.Thread1(res01, res02);
        DeadLock.Thread2 t2 = new DeadLock.Thread2(res01, res02);
        t1.start();
        t2.start();
    }
}


